# -*- coding: UTF-8 -*-
from django.http import *
from django.http import HttpResponseRedirect
from google.appengine.api import memcache
import utils

def is_user_login(request):
    return 'user' in request.COOKIES or 'oauth' in request.COOKIES

def require_login():
    return HttpResponseRedirect('/login')

def error():
    return HttpResponseRedirect('/error')

def index(request):
    if is_user_login(request):
        return HttpResponseRedirect('/home')
    else:
        return HttpResponseRedirect('/login')

def home(request):
    if is_user_login(request):
        api = utils.get_api(request)
        return utils.respond(request, 'home', {
                             'item_list': api.home_timeline(),
                             'me': utils.get_cached_verify_credentials(request),
                             'current_page': 'home',
                             'settings': utils.load_settings(request)
                             })
    else:
        return require_login()
    
def setting(request):
    if request.method == 'GET':
        return utils.respond(request, 'setting', {
                             'me': utils.get_cached_verify_credentials(request),
                             'settings': utils.load_settings(request)
                             })
    elif request.method == 'POST':
        response = HttpResponseRedirect("/setting")
        utils.save_settings(request, response)
        return response

def fav(request):
    api = utils.get_api(request)
    return utils.respond(request, 'mentioned', {
                         'item_list': api.favorites(),
                         'me': utils.get_cached_verify_credentials(request),
                         'current_page': 'fav',
                         'title': '你的收藏',
                         'subtitle': '你的收藏'
                         })

def dm(request, mode):
    if mode == '':
        mode = 'inbox'
    api = utils.get_api(request)
    if mode == 'inbox':
        dms = api.direct_messages()
    else:
        dms = api.sent_direct_messages()
    for dm in dms:
        setattr(dm, 'user', mode == 'inbox' and dm.sender or dm.recipient)
    return utils.respond(request, 'dm', {
                         'dms': dms,
                         'me': utils.get_cached_verify_credentials(request),
                         'current_page': 'dm',
                         'mode': mode
                         })
    
def user(request, screen_name):
    api = utils.get_api(request)
    user_timeline = api.user_timeline(screen_name=screen_name)
    user = len(user_timeline) and user_timeline[0].user or api.get_user(screen_name=screen_name)
    return utils.respond(request, 'user', {
                         'item_list': user_timeline,
                         'user': user,
                         'hide_avatar': True
                         })
    
def mentioned(request):
    api = utils.get_api(request)
    me = utils.get_cached_verify_credentials(request)
    return utils.respond(request, 'mentioned', {
                         'item_list': api.mentions(),
                         'me': me,
                         'current_page': 'mentioned',
                         'title': '@%s' % me.screen_name,
                         'subtitle': 'Tweets mentioning @%s' % me.screen_name
                         })

def status(request, status_id):
    if is_user_login(request):
        api = utils.get_api(request)
        return utils.respond(request, 'status', {'status': api.get_status(status_id)})
    else:
        return require_login()

def login(request):
    if request.method == 'GET':
        return utils.respond(request, 'login', None)
    else:
        resp = HttpResponseRedirect('/')
        resp.set_cookie('user', request.POST['username'])
        resp.set_cookie('pwd', request.POST['password'])
        return resp

def update(request):
    if request.method == 'POST':
        api = utils.get_api(request)
        api.update_status(
                          status=request.POST['status'],
                          in_reply_to_status_id=request.POST['in_reply_to']
                          )
        #memcache.delete('verify_cridentials')
        return HttpResponseRedirect('/')

def signout(request):
    response = HttpResponseRedirect('/')
    for key in request.COOKIES:
        response.delete_cookie(key)
    utils.clear_memcache(request)
    return response
